//
//  SampleTableViewController.swift
//  GenericsDemo
//
//  Created by 劢克科技 on 2019/3/14.
//  Copyright © 2019 劢克科技. All rights reserved.
//

import UIKit

class BasicTableViewController<T: BasicCell>: UITableViewController {
    
    let cellID = "cellID"
    var item = [Any]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.register(T.self, forCellReuseIdentifier: cellID)
    }
    
    // MARK: - Table view data source
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return item.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID) as! BasicCell
        cell.item = item[indexPath.row]
        return cell
    }
}

class BasicCell: UITableViewCell {
    var item: Any! {
        didSet {
            textLabel?.text = item as? String
        }
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        backgroundColor = .yellow
    }
}

class SampleTableViewController: BasicTableViewController<BasicCell> {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        item = ["Hello", "World", "Aowsome"]
    }
}
